﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace app
{
    static class Log
    {
        static internal void Add(string function, Peer peer, byte[] request)
        {
            Add(
                function,
                peer.Address,
                peer.IPEndPoint,
                request);
        }

        static internal void Add(string function, IPEndPoint IPEndPoint, byte[] request)
        {
            Add(function,
                new byte[Parameters.addressSize],
                IPEndPoint,
                request);
        }

        static private void Add(string function, byte[] address, IPEndPoint IPEndPoint, byte[] request)
        {
            function = function.Substring(0, Math.Min(7, function.Length));

            string r = string.Empty;

            if (request != null)
            {
                r = string.Join(".", request);

                if (r.Length > 39)
                {
                    r = string.Empty;
                    string s = string.Empty;

                    foreach (byte b in request)
                    {
                        r += b.ToString() + ".";

                        if (r.Length > 33)
                        {
                            s += r + "\r\n\t\t\t\t  ";
                            r = string.Empty;
                        }
                    }
                    s += r + "\r\n\t\t\t\t  ";
                    r = s.Remove(s.Length - 1);

                }
            }

            Console.WriteLine("{0}\t{1}\t{2}:{3}\t- {4}\r\n",
                function,
                string.Join(".", address),
                string.Join(".", IPEndPoint.Address.GetAddressBytes()),
                IPEndPoint.Port,
                r);
        }


    }
}
